package com.application.sistercityapplication.main;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.application.sistercityapplication.dto.MagazineDto;
import com.application.sistercityapplication.json.JSONParserForGetList;
import com.application.sistercityapplication.listadapter.KmsLoadedDocumentListAdapter;
import com.application.sistercityapplication.listadapter.LoadedDocumentListAdapter;
import com.application.sistercityapplication.listadapter.MouLoadedDocumentListAdapter;
import com.application.sistercityapplication.model.Magazine;
import com.application.sistercityapplication.model.User;
import com.application.sistercityapplication.utils.GlobalVariable;
import com.roscopeco.ormdroid.Entity;

public class MainNewsDockViewActivity extends Activity {
	private BookshelfViewForDocument bsView;
	private RelativeLayout layoutForGrid,layoutForList;
	private ImageView managementBtn,closeOptionBtn,selectAllElementBtn,changeModeBtn,deleteBtn,objectSelected,backIcon;
	private ListView listShelf;
	private int deleteState = 0; //0 not have permitted to delete, 1 have permitted
	private int modeChange = 0; //0 is shelf mode, 1 is list mode
	private BooksAdapter booksAdapter;
	private KMSAdapter kmsAdapter;
	private MOUAdapter mouAdapter;
	private int country_id;
	private int menuCase;
	@Override
	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_news_dock_view);
		System.gc();
		
		country_id = getIntent().getIntExtra("countryId", 0);
		menuCase = getIntent().getIntExtra("menu", 0);
		String country_name = getIntent().getStringExtra("countryName");
		
		bsView = (BookshelfViewForDocument) findViewById(R.id.documentShelf);
		layoutForGrid = (RelativeLayout) findViewById(R.id.layoutForGrid);
		layoutForList = (RelativeLayout) findViewById(R.id.layoutForList);
		//objectSelected = (ImageView) findViewById(R.id.objectSelected);
		backIcon = (ImageView) findViewById(R.id.back_icon);
		managementBtn = (ImageView) findViewById(R.id.managementBtn);
		closeOptionBtn = (ImageView) findViewById(R.id.closeOptionBtn);
		selectAllElementBtn = (ImageView) findViewById(R.id.selectAllElementBtn);
		changeModeBtn = (ImageView) findViewById(R.id.changeModeBtn);
		listShelf = (ListView) findViewById(R.id.list);
		deleteBtn = (ImageView) findViewById(R.id.deleteBtn);
		deleteBtn.setImageDrawable(getResources().getDrawable(R.drawable.del_doc_btn_notpermit));
		initMenu();
		changeModeBtn.setOnClickListener(new ChangeModeClickLestener());
		managementBtn.setOnClickListener(new ManageClickLListener());
		closeOptionBtn.setOnClickListener(new CloseOptionMenuListener());
		selectAllElementBtn.setOnClickListener(new SelectAllListener());
		layoutForList.setVisibility(View.GONE);
		new DownloadBooksTask(getApplicationContext(), bsView,MainNewsDockViewActivity.this,deleteState,deleteBtn).execute();
		backIcon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MainNewsDockViewActivity.this.finish();
			}
		});
		deleteBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(deleteState==99){
					Magazine mag = new Magazine();
					mag.removeAll();
				}else{
					List<Integer> removeList = new ArrayList<Integer>();
					switch (menuCase) {
					case 0:
						removeList = booksAdapter.getRemoveList();
					break;
					case 1:
						removeList = kmsAdapter.getRemoveList();
					break;
					case 2:
						removeList = mouAdapter.getRemoveList();
					break;
					default:
						break;
					}
					
					if(removeList!=null && !removeList.isEmpty()){
						for(Integer i : removeList)
						{
							Magazine mag =  Entity.query(Magazine.class).whereId(i).execute();
							mag.delete();
						}
						new DownloadBooksTask(getApplicationContext(), bsView,MainNewsDockViewActivity.this,deleteState,deleteBtn).execute();
					} 
			}
			
		}
	});
	}
	class DownloadBooksTask extends AsyncTask<String, Void, List<MagazineDto>> {
		ProgressDialog pDialog;
		private Context context;
		private BookshelfViewForDocument view;
		public Activity send;
		private int _deleteState;
		private ImageView _btnDel;

		public DownloadBooksTask(Context context, BookshelfViewForDocument view,Activity aa,int deleteState,ImageView btnDelete) {
			super();
			this.context = context;
			this.view = view;
			this.send = aa;
			this._deleteState = deleteState;
			this._btnDel = btnDelete;
		}
		public DownloadBooksTask(Activity aa,BookshelfViewForDocument view) {
			super();
			this.send = aa;
			this.context = aa;
			this.view = view;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(MainNewsDockViewActivity.this);
			pDialog.setMessage(GlobalVariable.LOADING_DIALOG_MESSAGE);
			pDialog.show();
		}

		@Override
		protected List<MagazineDto> doInBackground(String... params) {
//			Magazine mag = new Magazine();
//			User user = new User();
//			List<Magazine> listTest = mag.getAllByAuthen(user.isNotEmpty());
//			List<Magazine> listTest = new ArrayList<Magazine>();
//			mag = new Magazine();
//			mag.id = R.drawable.philipins;
//			listTest.add(mag);
//			mag = new Magazine();
//			mag.id =R.drawable.vietnam;
//			listTest.add(mag);
			List<MagazineDto> listTest = new ArrayList<MagazineDto>();
			switch (menuCase) {
			case 0:
				listTest = JSONParserForGetList.getInstance().getMagazines(country_id+"");
			break;
			case 1:
				listTest = JSONParserForGetList.getInstance().getKMS(country_id+"");
			break;
			case 2:
				listTest = JSONParserForGetList.getInstance().getMOU(country_id+"");
			break;
			default:
				break;
			}
			return listTest;
		}

		@Override
		protected void onPostExecute(List<MagazineDto> result) {
			super.onPostExecute(result);
			if (null != pDialog && pDialog.isShowing()) {
				pDialog.dismiss();
			}

			if (result != null) {
				
				switch (menuCase) {
				case 0:
					booksAdapter = new BooksAdapter(context, result, send,_deleteState,_btnDel);
					view.setAdapter(booksAdapter);
				break;
				case 1:
					kmsAdapter = new KMSAdapter(context, result, send,_deleteState,_btnDel);
					view.setAdapter(kmsAdapter);
				break;
				case 2:
					mouAdapter = new MOUAdapter(context, result, send,_deleteState,_btnDel);
					view.setAdapter(mouAdapter);
				break;
				default:
					break;
				}
			} else {
				(Toast.makeText(context, "An error occured.", Toast.LENGTH_LONG))
						.show();
			}

		}

	}
	public class ManageClickLListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			showOptionMenu();
			if(modeChange==0){
				deleteState =1;
				new DownloadBooksTask(getApplicationContext(), bsView,MainNewsDockViewActivity.this,deleteState,deleteBtn).execute();
			}else{
				deleteState =1;
				new DownloadBooksListTask(getApplicationContext(),listShelf,MainNewsDockViewActivity.this,deleteState,deleteBtn).execute();
			}
		}
		
	}
	public void initMenu(){
		closeOptionBtn.setVisibility(View.INVISIBLE);
		selectAllElementBtn.setVisibility(View.INVISIBLE);
		deleteBtn.setVisibility(View.INVISIBLE);
		
		managementBtn.setVisibility(View.VISIBLE);
		changeModeBtn.setVisibility(View.VISIBLE);
	}
	public void showOptionMenu(){
		closeOptionBtn.setVisibility(View.VISIBLE);
		selectAllElementBtn.setVisibility(View.VISIBLE);
		deleteBtn.setVisibility(View.VISIBLE);
		
		managementBtn.setVisibility(View.INVISIBLE);
		changeModeBtn.setVisibility(View.INVISIBLE);
	}
	public class ChangeModeClickLestener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(modeChange==0){
				modeChange =1;
				changeModeBtn.setImageDrawable(getResources().getDrawable(R.drawable.shelf_doc_shelf_icon));
				layoutForGrid.setVisibility(View.INVISIBLE);
				layoutForList.setVisibility(View.VISIBLE);
				managementBtn.setVisibility(View.INVISIBLE);
				new DownloadBooksListTask(getApplicationContext(),listShelf,MainNewsDockViewActivity.this,deleteState,deleteBtn).execute();
			}else if(modeChange==1){
				modeChange=0;
				changeModeBtn.setImageDrawable(getResources().getDrawable(R.drawable.list_doc_shelf_icon));
				layoutForGrid.setVisibility(View.VISIBLE);
				layoutForList.setVisibility(View.INVISIBLE);
				managementBtn.setVisibility(View.VISIBLE);
				new DownloadBooksTask(getApplicationContext(), bsView,MainNewsDockViewActivity.this,deleteState,deleteBtn).execute();
			}

		}
		
	}
	public class CloseOptionMenuListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			initMenu();
			deleteState =0;
			if(modeChange==0){
				deleteBtn.setImageDrawable(getResources().getDrawable(R.drawable.del_doc_btn_notpermit));
				new DownloadBooksTask(getApplicationContext(), bsView,MainNewsDockViewActivity.this,deleteState,deleteBtn).execute();
			}else{
				new DownloadBooksListTask(getApplicationContext(),listShelf,MainNewsDockViewActivity.this,deleteState,deleteBtn).execute();
			}
		}
		
	}
	public class SelectAllListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			deleteState =99;
			if(modeChange==0){
				deleteBtn.setImageDrawable(getResources().getDrawable(R.drawable.del_doc_btn));
				new DownloadBooksTask(getApplicationContext(), bsView,MainNewsDockViewActivity.this,deleteState,deleteBtn).execute();
			}else{
				deleteBtn.setImageDrawable(getResources().getDrawable(R.drawable.del_doc_btn));
				new DownloadBooksListTask(getApplicationContext(),listShelf,MainNewsDockViewActivity.this,deleteState,deleteBtn).execute();
			}
		}
		
	}
	public class DownloadBooksListTask extends AsyncTask<String, Void, List<MagazineDto>> {
		ProgressDialog pDialog;
		private Context context;
		private ListView view;
		public Activity send;
		private int _deleteState;
		private ImageView _btnDel;

		public DownloadBooksListTask(Context context, ListView view,Activity aa,int deleteState,ImageView btnDelete) {
			super();
			this.context = context;
			this.view = view;
			this.send = aa;
			this._deleteState = deleteState;
			this._btnDel = btnDelete;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(MainNewsDockViewActivity.this);
			pDialog.setMessage(GlobalVariable.LOADING_DIALOG_MESSAGE);
			pDialog.show();
		}

		@Override
		protected List<MagazineDto> doInBackground(String... params) {
			//List<Magazine> listTest = Entity.query(Magazine.class).executeMulti();
//			Magazine mag = new Magazine();
//			User user = new User();
//			List<Magazine> listTest = mag.getAllByAuthen(user.isNotEmpty());
			List<MagazineDto> listTest = new ArrayList<MagazineDto>();
			switch (menuCase) {
			case 0:
				listTest = JSONParserForGetList.getInstance().getMagazines(country_id+"");
			break;
			case 1:
				listTest = JSONParserForGetList.getInstance().getKMS(country_id+"");
			break;
			case 2:
				listTest = JSONParserForGetList.getInstance().getMOU(country_id+"");
			break;
			default:
				break;
			}
			return listTest;
		}

		@Override
		protected void onPostExecute(List<MagazineDto> result) {
			super.onPostExecute(result);
			if (null != pDialog && pDialog.isShowing()) {
				pDialog.dismiss();
			}

			if (result != null) {
				switch (menuCase) {
				case 0:
					view.setAdapter(new LoadedDocumentListAdapter(result,context, send,_deleteState,_btnDel));
				break;
				case 1:
					view.setAdapter(new KmsLoadedDocumentListAdapter(result,context, send,_deleteState,_btnDel));
				break;
				case 2:
					view.setAdapter(new MouLoadedDocumentListAdapter(result,context, send,_deleteState,_btnDel));
				break;
				default:
					break;
				}
				
				
			} else {
				
			}

		}

	}
}
